package com.madi.company.ui.main.adapter;

import android.animation.Animator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aspsine.irecyclerview.animation.AlphaInAnimation;
import com.aspsine.irecyclerview.animation.BaseAnimation;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.madi.company.R;
import com.madi.company.bean.ProfessionTypeEntity;
import com.madi.company.presenter.ProfessionTypePresenter;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.List;
import java.util.Map;



/**
 * 行业类别 adapter
 * Created by 丁宁
 * on 2016/12/9.
 */

public class ProfessionTypeAdapter extends RecyclerView.Adapter<ProfessionTypeAdapter.MyViewHolder> implements StickyRecyclerHeadersAdapter {
    private List<ProfessionTypeEntity> Datas;
    private Context mContext;
    //动画
    private int mLastPosition = -1;
    private boolean mOpenAnimationEnable = true;
    private Interpolator mInterpolator = new LinearInterpolator();
    private int mDuration = 300;
    private BaseAnimation mSelectAnimation = new AlphaInAnimation();
    private List<ProfessionTypeEntity> mParentData;
    private Map<String, Integer> map ;
    private ProfessionTypePresenter mPresenter;

    /**
     * @param context 上下文
     * @param data    行业类别子数据
     * @param datas   行业类别父数据
     */
    public ProfessionTypeAdapter(Context context, List<ProfessionTypeEntity> data, List<ProfessionTypeEntity> datas, ProfessionTypePresenter mPresenter, Map<String, Integer> map) {
        this.mContext = context;
        this.Datas = data;
        this.mParentData = datas;
        this.mPresenter = mPresenter;
        this.map = map;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_profession, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final ProfessionTypeEntity entity = Datas.get(position);
        holder.tv.setText(entity.getTradeTypeName());

        if (map != null) {
            // 下次进入时增加需要的选中状态
            for (Map.Entry entry : map.entrySet()) {
                String key = (String) entry.getKey();
                if (entity.getTradeTypeName().equals(key)) {
                    entity.setIsChecked(true);
                }
            }
        }

        holder.mCheckBox.setChecked(entity.getIsChecked());
        // item 的点击事件
        holder.mLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entity.getIsChecked()) {
                    // 确定状态点击为false
                    holder.mCheckBox.setChecked(false);
                    entity.setIsChecked(false);
                    map.remove(entity.getTradeTypeName());
                } else {
                    if (map.size() < 5) {
                        entity.setIsChecked(true);
                        holder.mCheckBox.setChecked(true);
                        map.put(entity.getTradeTypeName(), entity.getId());
                    } else {
                        ToastUitl.showShort(mContext.getResources().getString(R.string.reach_the_limit));
                    }
                }
                mPresenter.getSelectedData(map);
            }
        });
        addAnimation(holder);
    }

    @Override
    public int getItemCount() {
        return Datas.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // viewholder
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;// ps-计算机软件..
        LinearLayout mLinear;
        CheckBox mCheckBox;

        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.title);
            mLinear = (LinearLayout) view.findViewById(R.id.linear);
            mCheckBox = (CheckBox) view.findViewById(R.id.title_check);
        }
    }

    /**
     * 这一块 就是返回值相同 代表的就是一类（parentid相同的情况下就是一类的） 小婊砸
     *
     * @param position
     * @return
     */
    @Override
    public long getHeaderId(int position) {
        ProfessionTypeEntity entity = Datas.get(position);
        return entity.getParentId();
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_header, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView textView = (TextView) holder.itemView.findViewById(R.id.tvHeader);
        for (ProfessionTypeEntity entity : mParentData) {
            if (entity.getId() == Datas.get(position).getParentId()) {
                textView.setText(entity.getTradeTypeName());
            }
        }

    }

    /**
     * 添加动画
     *
     * @param holder
     */
    public void addAnimation(RecyclerView.ViewHolder holder) {
        if (mOpenAnimationEnable) {
            if (holder.getLayoutPosition() > mLastPosition) {
                BaseAnimation animation = null;
                if (mSelectAnimation != null) {
                    animation = mSelectAnimation;
                }
                for (Animator anim : animation.getAnimators(holder.itemView)) {
                    startAnim(anim, holder.getLayoutPosition());
                }
                mLastPosition = holder.getLayoutPosition();
            }
        }
    }

    /**
     * 开始动画
     *
     * @param anim
     * @param index
     */
    protected void startAnim(Animator anim, int index) {
        anim.setDuration(mDuration).start();
        anim.setInterpolator(mInterpolator);
    }

    /**
     * 设置动画
     *
     * @param animation ObjectAnimator
     */
    public void openLoadAnimation(BaseAnimation animation) {
        this.mOpenAnimationEnable = true;
        this.mSelectAnimation = animation;
    }

    public void updateListView(List<ProfessionTypeEntity> list) {
        this.Datas = list;
        notifyDataSetChanged();
    }
    /**
     * @param key
     */
    public void notifyData(String key) {
        for (ProfessionTypeEntity entity : Datas){
            if (key.equals(entity.getTradeTypeName())){
                entity.setIsChecked(false);
            }
        }
        notifyDataSetChanged();
    }
}
